﻿<template>
  <a-drawer
    title="收货"
    width="90%"
    placement="right"
    :visible="visible"
    @close="
      () => {
        this.visible = false
      }
    "
    :body-style="{ paddingBottom: '80px' }"
  >
    <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="收货单号" prop="Code">
            <a-input
              v-model="entity.Id"
              :disabled="$para('GenerateInStorageCode') == '1' || disabled"
              placeholder="系统自动生成"
              autocomplete="off"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="订单时间" prop="OrderTime">
            <a-date-picker v-model="entity.OrderTime" :style="{ width: '100%' }" :disabled="disabled" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="收货时间" prop="RecTime">
            <a-date-picker v-model="entity.RecTime" :style="{ width: '100%' }" :disabled="disabled" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="收货类型" prop="Type">
            <enum-select code="ReceiveType" v-model="entity.Type" :disabled="disabled"></enum-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="入库通知单号" prop="RefCode">
            <a-input v-model="entity.RefCode" autocomplete="off" :disabled="disabled" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="供货单位" prop="SupId">
            <sup-select v-model="entity.SupId" :disabled="disabled"></sup-select>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="收货仓库" prop="StorId">
            <a-tree-select
              allowClear
              :treeData="ParentIdTreeData"
              placeholder="请选择收货仓库"
              @change="SelectOrgan"
              v-model="entity.StorId"
              :disabled="disabled"
            ></a-tree-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item label="备注" prop="Remarks">
            <a-input v-model="entity.Remarks" :disabled="disabled" :style="{ width: '100%' }" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8" v-if="entity.OrderId">
          <a-form-model-item label="关联订单号" prop="OrderId">
            <a-input v-model="entity.OrderId" :disabled="disabled" :style="{ width: '100%' }" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <list-detail v-model="listDetail" :disabled="disabled"></list-detail>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <!-- <a-button type="primary" @click="oks(false)" style=" margin-right: 15px;"
      >审核打回</a-button
      > -->
      <a-button type="primary" v-if="title == '审核'" @click="oks(true)">确认通过</a-button>
      <a-button type="primary" v-if="title == '提交'" @click="butsub">提交</a-button>
      <a-button type="primary" v-if="title == '复核'" @click="reexamine">复核通过</a-button>
      <a-button type="danger " v-if="title == '复核'" @click="reexaminess" style="margin-left: 10px">复核退回</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import EnumSelect from '../../../components/BaseEnum/BaseEnumSelect'
// import EnumName from '../../../components/BaseEnum/BaseEnumName'
// import SupSelect from '../../../components/PB/SupplierSelect'
import ListDetail from '../TD_RecDetail/List'
import moment from 'moment'
// import Organization from '@/components/SelectOrganization/Organization'

export default {
  components: {
    // EnumSelect,
    // EnumName,
    // SupSelect,
    ListDetail,
    // Organization
  },
  props: {
    parentObj: { type: Object, required: true },
    disabled: { type: Boolean, required: false, default: false }
  },
  data() {
    return {
      ParentIdTreeData: [],
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      pid: '',
      title: '',
      idIn: '',
      judge: false,
      loading: false,
      entity: { Id: '', Status: 0 },
      listDetail: [],
      listdata: [],
      organ: '',
      rules: {
        OrderTime: [{ required: true, message: '请输入订单时间', trigger: 'change' }],
        RecTime: [{ required: true, message: '请输入收货时间', trigger: 'change' }],
        Type: [{ required: true, message: '请选择收货入库类型', trigger: 'change' }],
        StorId: [{ required: true, message: '请选择收货仓库', trigger: 'change' }]
        //SupId: [{ required: true, message: '请选择供货单位', trigger: 'change' }]
      }
    }
  },
  methods: {
    reexaminess() {
      this.$confirm({
        title: '确认复核退回吗?',
        onOk: () => {
          this.$http.post('/TD/TD_Receiving/UpReceReturn?receivingId=' + this.pid).then(res => {
            if (res.Success) {
              this.$message.success('退回成功!')
              this.visible = false

              this.parentObj.getDataList()
            } else {
              this.$message.error(res.Msg)
            }
          })
        }
      })
    },
    reexamine() {
      this.$confirm({
        title: '确认复核通过吗?',
        onOk: () => {
          this.$http.post('/TD/TD_Receiving/SubmitLocalMaterial?receivingId=' + this.pid).then(res => {
            if (res.Success) {
              this.$message.success('复核成功!')
              this.visible = false

              this.parentObj.getDataList()
            } else {
              this.$message.error(res.Msg)
            }
          })
        }
      })
    },
    butsub() {
      this.$confirm({
        title: '确认提交吗?',
        onOk: () => {
          this.$http.post('/TD/TD_Receiving/SubmitReceing?receivingId=' + this.pid).then(res => {
            if (res.Success) {
              this.$message.success('提交成功!')
              this.visible = false

              this.parentObj.getDataList()
            } else {
              this.$message.error(res.Msg)
            }
          })
        }
      })
    },
    moment,
    init() {
      this.visible = true
      this.entity = { Id: '', Status: 0, OrderTime: moment() }
      this.listDetail = []
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    organs() {
      setTimeout(() => {
        this.$refs.Organization.Organ()
      }, 100)
    },
    openForm(id, tit) {
      this.init()
      this.SelectOrgan()
      this.pid = id
      this.title = tit
      // this.organs()
      if (id) {
        this.loading = true
        this.$http.post('/TD/TD_Receiving/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
          this.entity.OrderTime = moment(this.entity.OrderTime)
          this.entity.RecTime = moment(this.entity.RecTime)
          this.listDetail = [...resJson.Data.RecDetails]
          this.organ = resJson.Data.StorId
          console.log(this.listDetail)
          this.listdata = resJson.Data.RecDetails
          // this.listDetail.forEach((item) => {
          //   if (item.StartDate) item.StartDate = moment(item.StartDate)
          //   if (item.EndDate) item.EndDate = moment(item.EndDate)

          //   // item.EndDate = moment(item.EndDate)
          // })
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        // 数据处理
        var RecDetails = [...this.listDetail]
        RecDetails.forEach(element => {
          element.Material = null
        })
        var entityData = { ...this.entity }
        console.log(this.entity)
        entityData.RecDetails = RecDetails
        this.$http.post('/TD/TD_Receiving/SaveData', entityData).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
    async SelectOrgan() {
      await this.$http.post('/View/View_Base_Department/GetTreeTypeDataLists', { Type: 2 }).then(resJson => {
        if (resJson.Success) {
          this.ParentIdTreeData = resJson.Data
        }
      })
    },
    oks(val) {
      console.log(this.listdata)
      var ok = ''
      if (val == true) {
        ok = true
      } else {
        ok = false
      }
      var arr = []
      this.listdata.forEach(Item => {
        arr.push({
          MaterialId: Item.MaterialId,
          RecNum: Item.PlanNum
        })
      })
      var obj = {
        idIn: this.pid,
        judge: true,
        MaterialList: arr
      }
      let Validity = this.listDetail.filter(val => {
        return val.Material.IsPeriodOfValidity == true
      })

      var date = this.listDetail.some(item => {
        return item.StartDate == null || item.EndDate == null
      })
      let End = Validity.filter(it => {
        return it.EndDate == null && it.StartDate == null
      })
      if (date && Validity.length > 0 && End.length > 0) {
        this.$message.error('请先编辑,补充日期')
        return
      }

      this.$http.post('/TD/TD_Receiving/SaveToExamine', obj).then(resJson => {
        console.log(resJson)
        this.loading = false
        this.visible = false
        if (resJson.Success) {
          this.$message.success('确认成功')
        } else {
          this.$message.error(resJson.Msg)
        }
        this.parentObj.getDataList()
      })
    },
    falseok(val) {
      var ok = ''
      if (val == false) {
        ok = false
      } else {
        ok = true
      }
      this.$http.post('/TD/TD_Receiving/SaveToExamine?idIn=' + this.pid + '&judge=' + ok).then(resJson => {
        console.log(resJson)
        this.loading = false
        this.visible = false

        if (val == true) {
          this.$message.success('审核失败')
          console.log(this.entity)
          this.entity.Status = '2'
        } else {
          this.$message.error('审核成功')
        }
      })
    },
    handleAudit(id, type) {
      this.loading = true
      this.$http.post('/TD/TD_Receiving/Approval', { Id: id, AuditType: type }).then(resJson => {
        this.loading = false
        if (resJson.Success) {
          this.$message.success('操作成功!')
          this.visible = false
          this.parentObj.getDataList()
        } else {
          this.$message.error(resJson.Msg)
        }
      })
    }
  }
}
</script>
